﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace DIProject.Data
{
    public class Sync
    {
        private static object _isOffice = null;
        /// <summary>
        /// определяем мы в оффисе или или аптеке.
        /// </summary>
        public static bool IsOffice
        {
            get
            {
                if (_isOffice != null)
                    return (bool)_isOffice;
                string conStr = Properties.Settings.Default.DIAptekaConnectionString;
                string QueryString = 
@"select top 1 DepartmentId from PR.DepartmentOptions
order by DepartmentId";
                using (SqlConnection connection = new SqlConnection(conStr))
                {
                    try
                    {
                        SqlCommand command = new SqlCommand(QueryString, connection);
                        connection.Open();
                        object obj = command.ExecuteScalar();
                        if (Convert.IsDBNull(obj))
                        {
                            _isOffice = false;
                            return false;
                        }
                        Int32 tmp = (Int32)obj;
                        if (tmp == 0)
                        {
                            _isOffice = true;
                            return true;
                        }
                        _isOffice = false;
                        return false;
                    }
                    catch (SqlException se)
                    {
                        throw new DIProject.Util.DIException("Ошибка при получении признака оффиса", se);
                    }
                    catch (Exception ee)
                    {
                        throw new DIProject.Util.DIException("Ошибка при получении признака оффиса", ee);
                    }
                }
            }
        }
        private static object _isAdmin = null;
        /// <summary>
        /// определяем админ или нет
        /// </summary>
        public static bool IsAdmin
        {
            get
            {
                if (_isAdmin != null)
                    return (bool)_isAdmin;
                string QueryString =
    @"SELECT TOP (1) UserId
FROM  dbo.vwDIRoleMembers
WHERE (RoleName = N'DIAdmin')";
                string QueryString1 =
    @"SELECT     TOP (1) UserId
FROM         vwDIRoleMembers
WHERE (UserName = @UserName) AND (RoleName = 'DIAdmin')";
                string conStr = Properties.Settings.Default.DIAptekaConnectionString;
                using (SqlConnection connection = new SqlConnection(conStr))
                {
                    try
                    {
                        SqlCommand command = new SqlCommand(QueryString1, connection);
                        SqlParameter userName = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
                        userName.Value = FirstForm.Login;
                        command.Parameters.Add(userName);
                        connection.Open();
                        object obj = command.ExecuteScalar();
                        if (Convert.IsDBNull(obj)||(obj==null))
                        {
                            _isAdmin = false;
                            return false;
                        }
                        _isAdmin = true;
                        return true;
                    }
                    catch (SqlException se)
                    {
                        throw new DIProject.Util.DIException("Ошибка при получении признака администратора", se);
                    }
                    catch (Exception ee)
                    {
                        throw new DIProject.Util.DIException("Ошибка при получении признака администратора", ee);
                    }
                }
            }
        }
        public static void SetVisible(DataTable data)
        {
            if (!data.Columns.Contains("ToDelete"))
            {
                throw new Util.DIException("Не найден столбец ToDelete");
            }
            data.DefaultView.RowFilter = "ToDelete = 0";
            PrintTable(data);
        }
        public static void ClearVisible(DataTable data)
        {
            if (!data.Columns.Contains("ToDelete"))
            {
                throw new Util.DIException("Не найден столбец ToDelete");
            }
            data.DefaultView.RowFilter = string.Empty;
        }
        private static void PrintTable(DataTable data)
        {
            foreach (DataRow dr in data.Rows)
            {
                for (Int32 i = 0; i < dr.ItemArray.Length; i++)
                {
                    Debug.Write(dr.ItemArray[i]);
                    
                    Debug.Write("\t");
                }
                Debug.Write("\n");
            }
        }
    }
}
